package codecs

type AmrPayloader struct{}

func (p *AmrPayloader) PayloadAmr(amr_data []byte) (rtp_payload [][]byte) {
	amr_data = amr_data[6:]
	amr_data = Bytes2Bits(amr_data)
	for index := 0; index < len(amr_data); {
		payload := make([]byte, 0)
		payload = append(payload, CMR...)                       //CMR
		payload = append(payload, 0)                            //F
		payload = append(payload, amr_data[index+1:index+6]...) //FT和Q
		flen := amr_nbFrameLen[int((amr_data[index+1]<<3)+(amr_data[index+2]<<2)+(amr_data[index+3]<<1)+amr_data[index+4])]
		payload = append(payload, amr_data[index+8:index+flen+8]...) //payloaddata
		for num := 8 - len(payload)%8; 0 < num && num < 8; num-- {   //8位对齐补0
			payload = append(payload, 0)
		}
		rtp_payload = append(rtp_payload, Bits2Bytes(payload))
		index += 8 + flen
		for num := 8 - flen%8; 0 < num && num < 8; num-- {
			index += 1
		}
	}
	return rtp_payload
}

func (p *AmrPayloader) PayloadAmrWB(amr_data []byte) (rtp_payload [][]byte) {
	var frame []byte
	amr_data = amr_data[9:]
	for current, length := 0, len(amr_data); current < length; {
		index := 0
		ft := amr_data[current] >> 3
		next := current + wbSpeechFrameBytes[ft]
		frame = make([]byte, wbSpeechFrameBytes[ft])
		frame[index] = (8 << 4) | (amr_data[current] >> 4)
		frame[index+1] = (amr_data[current] << 4) | (amr_data[current+1] >> 2)
		index += 2
		for current += 2; current < next; current++ {
			frame[index] = (amr_data[current-1] << 6) | (amr_data[current] >> 2)
			index++
		}
		rtp_payload = append(rtp_payload, frame)
	}
	return rtp_payload
}

// amr整个文件拷贝...
func (p *AmrPayloader) PayloadAmrOctet(amr_data []byte) (rtp_payload [][]byte) {
	amr_data = amr_data[6:]
	amr_data = Bytes2Bits(amr_data)
	for index := 0; index < len(amr_data); {
		payload := make([]byte, 0)
		payload = append(payload, CMR...)                       //CMR
		payload = append(payload, 0, 0, 0, 0)                   //PPPP
		payload = append(payload, 0)                            //F
		payload = append(payload, amr_data[index+1:index+6]...) //FT和Q
		payload = append(payload, 0, 0)
		flen := amr_nbFrameLen[int((amr_data[index+1]<<3)+(amr_data[index+2]<<2)+(amr_data[index+3]<<1)+amr_data[index+4])]
		payload = append(payload, amr_data[index+8:index+flen+8]...) //payloaddata
		for num := 8 - flen%8; 0 < num && num < 8; num-- {           //8位对齐补0
			payload = append(payload, 0)
			index += 1
		}
		rtp_payload = append(rtp_payload, Bits2Bytes(payload))
		index += 8 + flen
	}
	return rtp_payload
}

// amr整个文件拷贝...
func (p *AmrPayloader) PayloadAmrWbOctet(amr_data []byte) (rtp_payload [][]byte) {
	var frame []byte
	amr_data = amr_data[9:]
	speechHeader := 8 << 4
	for current, length := 0, len(amr_data); current < length; {
		ft := amr_data[current] >> 3
		next := current + wbSpeechFrameBytes[ft]
		frame = make([]byte, wbSpeechFrameBytes[ft]+1)
		frame[0] = byte(speechHeader)
		copy(frame[1:], amr_data[current:next])
		rtp_payload = append(rtp_payload, frame)
		current = next
	}
	return rtp_payload
}

// for amrnb com. test:
// TODO:
type AmrNbPayloader struct{}

func (p *AmrNbPayloader) Payload(amr_data []byte) (rtp_payload [][]byte) {
	amr_data = amr_data[6:]
	amr_data = Bytes2Bits(amr_data)
	for index := 0; index < len(amr_data); {
		payload := make([]byte, 0)
		payload = append(payload, CMR...)                       //CMR
		payload = append(payload, 0)                            //F
		payload = append(payload, amr_data[index+1:index+6]...) //FT和Q
		flen := amr_nbFrameLen[int((amr_data[index+1]<<3)+(amr_data[index+2]<<2)+(amr_data[index+3]<<1)+amr_data[index+4])]
		payload = append(payload, amr_data[index+8:index+flen+8]...) //payloaddata
		for num := 8 - len(payload)%8; 0 < num && num < 8; num-- {   //8位对齐补0
			payload = append(payload, 0)
		}
		rtp_payload = append(rtp_payload, Bits2Bytes(payload))
		index += 8 + flen
		for num := 8 - flen%8; 0 < num && num < 8; num-- {
			index += 1
		}
	}
	return rtp_payload
}
